$(document).ready(function () {
	
	
	function loadMyCart() {
		$.ajax({
			cache:true,
			type:"GET",
			url:"/dingcan/dingcan/listCard.do",
			async:true,
			error:function(data) {
				alert(data);
			},
			success:function(data) {
				var cart = $.parseJSON(data).data;
				/*
				 * JSON object to String: JSON.stringify(obj)
				 * String to JSON: JSON.parse(str);
				 */
				var result = cart.foods;
				var myfoodInfo = $("#myfoodInfo");
				var html = "";
				for(var i = 1; i <= result.length; i++) {
					var food = result[i-1];
					if(food == null) {
						continue;
					}
					
					html += '<table cellpadding="0" cellspacing="0" class="gwc_tb2" id="table'+ i +'">' + 
					'<tr>' + 
					  '<td class="tb2_td1"><input type="checkbox"  value="'+ food.foodPrice +'" name="newslist" oid='+ food.oid +' /></td>' + 
					  '<td class="tb2_td2"><a href="detailsp.html" target="_blank"><img src="'+ food.pics[0] +'"/></a></td> ' + 
					  '<td class="tb2_td3"><a href="detailsp.html" target="_blank">'+ food.foodName +'</a></td>' + 
					  '<td class="tb1_td4">￥'+ food.foodPrice +'</td> ' + 
					  '<td class="tb1_td7"><a href="/dingcan/dingcan/removeCard.do?oid='+ food.oid +'">删除</a></td>'+
					'</tr> </table> ';
					
				}
				myfoodInfo.html(html);
				
				// 输出
				$(".gwc_tb2 input[name=newslist]").click(function () {
					GetCount();
				});
			}
		});
	}

	loadMyCart();
	
	// 全选        
	$(".allselect").click(function () {
		if($(".allselect").attr("checked", true)) {
			$(".gwc_tb2 input[name=newslist]").each(function () {
					$(this).attr("checked", true);
			});
			$("#cancel").attr("checked", false);
		}
		GetCount();
	});

	//反选
	$("#invert").click(function () {
		$(".gwc_tb2 input[name=newslist]").each(function () {
			if ($(this).attr("checked")) {
				$(this).attr("checked", false);

			} else {
				$(this).attr("checked", true);

			} 
		});
		GetCount();
	});
	//取消
	$("#cancel").click(function () {
		if($("#cancel").attr("checked")) {
			$(".gwc_tb2 input[name=newslist]").each(function () {
					$(this).attr("checked", false);
			});
			$(".allselect").attr("checked", false);
			
		}
		GetCount();
	});

	
});

//******************
function GetCount() {
	var conts = 0;
	var aa = 0;
	var ids = "";
	$(".gwc_tb2 input[name=newslist]").each(function () {
		if ($(this).attr("checked")) {
			for (var i = 0; i < $(this).length; i++) {
				conts += parseInt($(this).val());
				aa += 1;
				ids += $(this).attr("oid") + ",";
			}
		}
	});
	ids = ids.substring(0, ids.length - 1);
	$("#shuliang").text(aa);
	$("#zong1").html((conts).toFixed(2));
	$("#jz1").css("display", "none");
	$("#jz2").css("display", "block");
	$("#jz2").attr("href","/dingcan/dingcan/countCard.do?ids=" + ids);
}
//ADD:对删除链接进行处理2014-9-20DeathGhost
    $(document).ready(function(){
		$("#delcart1").click(function(){
			$("#table1").remove();
			});
		$("#delcart2").click(function(){
			$("#table2").remove();
			});
		});
//<!---商品加减算总数---->
	
function setTotal() {
	$("#total1").html((parseInt(t.val()) * 59).toFixed(2));
	$("#newslist-1").val(parseInt(t.val()) * 59);
}
	